package com.ity.teamanagement.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        //是否支持cookie跨域
        config.setAllowCredentials(true);
        //设置允许域名
        config.setAllowedOrigins(Arrays.asList("*"));
        //设置允许头
        config.setAllowedHeaders(Arrays.asList("*"));
        //设置允许方法
        config.setAllowedMethods(Arrays.asList("*"));

        config.addExposedHeader("Authorization");

        //设置相同的跨域请求多少时间内不在进行判断
        config.setMaxAge(300L);
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
